var prefix = "/yw/settlement";
$(function () {
    createDate();
});

function createDate() {
    laydate.render({
        elem: '#settlementDate'
        , type: 'month'
        , value: new Date()
    });
    laydate.render({
        elem: '#settlementDateMore'
        , range: true
    });
}

function settlement(item) {
    var date1 = $('#settlementDate').val();
    var date2 = $('#settlementDateMore').val();
    if (item === 1) {
        if (date1) {
            console.log(date1);
            save(date1)
        } else {
            layer.msg("请选择月份")
        }

    } else if (item === 2) {
        if (date2) {
            console.log(date2);
            save(date2)
        } else {
            layer.msg("请选择时间段")
        }
    } else {
        layer.msg("页面出错请重新进入")
    }
}

function save(date) {
    var userId = $('#userId').val();
    $.ajax({
        cache: true,
        type: "GET",
        url: prefix + "/user/" + userId,
        data: {
            settlement: date
        },
        async: false,
        error: function () {
            parent.layer.msg("Connection error");
        },
        success: function (data) {
            if (data.code === 0) {
                parent.layer.msg("结算成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.msg(data.msg);
            }
        }
    });
}
